<?php
// 本类由系统自动生成，仅供测试用途
class ArticleAction extends AdminAction{

	public function _initialize() {
        parent::_initialize();  //RBAC 验证接口初始化
    }

	private $root_name='';
	private $site_root='';
	private $file_noexist = 'file_noexist';
    private $dele_statice_success = 'dele_statice_success';
    private $dele_statice_fail = 'dele_statice_fail';
    private $delete_success = 'delete_success';
    private $delete_fail = 'delete_fail';
    private $statice_success = 'statice_success';
    private $statice_fail = 'staice_fail';
    private $pro_no_statice_name = 'pro_no_statice_name';
    private $art_no_statice_name = 'art_no_statice_name';
    private $howto_no_statice_name = 'howto_no_statice_name';
    private $plate_no_statice_name = 'plate_no_statice_name';
    public function __construct(){
    	$this->root_name = 'http://'.$_SERVER['HTTP_HOST'].'/index.php';
    	$this->site_root = 'http://'.$_SERVER['HTTP_HOST'];
    	parent::__construct();

    }

	//文章分类管理
    public function article_classify_manage(){

    	$article_classify=M('ArticleClassify');
    	$cate=$article_classify->field('think_article_classify.*,b.big_category_name')->join('left join think_bigcategory b on b.id=think_article_classify.pid')->select();
    	$this->assign('cate',$cate);
    	$this->display();
    }

	//添加文章分类
	public function article_classify(){

		if($this->isPost()){
			$article_classify=M('ArticleClassify');
    		$_POST['add_time']=time();
    		if(!$_POST['classify_name'] || !$_POST['pid']){
    			return;
    		}
			$updata=$this->up(0,0);
    		if($updata){
    			foreach($updata as $v){
    				if($v['key'] == 'bgimg'){
    					$_POST['bgimg']=$v['savename'];
    				}elseif($v['key'] == 'img'){
    					$_POST['img']=$v['savename'];
    				}
    			}
    		}
			if($article_classify->where("pid=".$_POST['pid']." and classify_name='".$_POST['classify_name']."'")->find()){
    			echo "<script>alert('文章分类名已存在，请填写其他分类名');history.back();</script>";
    			return;
    		}
    		if($article_classify->add($_POST)){
    			echo "<script>alert('添加成功');location.href='".__URL__."/article_classify_manage';</script>";
    		}else{
    			echo "<script>alert('添加失败');history.back();</script>";
    		}

    	}else{
    		$bigcategory=M('Bigcategory');
    		$pro=M('Product');
    		$cate=$bigcategory->select();
    		$this->assign('cate',$cate);
    		$prodata = $pro->select();
	     	$this->assign('pdata',$prodata);
    		$this->display();
    	}
    }


	//文章管理
	public function article_manage(){
		$author=$_SESSION['username'];
		if($author=='admin'){
			echo "<script>location.href='".__URL__."/super_article_manage';</script>";
			return;
		}
    	$article=M('Article');
    	$bigcategory=M('Bigcategory');
    	$cate=$bigcategory->select();
    	$this->assign('cate',$cate);
    	$plate=M('Plate');
    	$pid=$_GET['pid'];
    	if($a=$article->where("author='".$author."'")->select()){
    		if(isset($pid) && $pid != ''){
    		$adata=$article->where('pid='.$pid,'author='.$author)->order('pid')->select();
    		$this->assign('b_id',$pid);
	    	}else{
	    		$adata=$article->where("author='".$author."'")->select();

	    	}
    	}else{
    		echo "<script>location.href='".__URL__."/super_article_manage';</script>";
    		return;
    		
    	}
    	foreach ($adata as $k => $v){
	    	$adata[$k]['cate_data']=$bigcategory->where('id='.$v['pid'])->find();
//    		$adata[$k]['plate_data']=$plate->where('id='.$v['pid'])->find();
    	}
    	$this->assign('product',$adata);
    	$this->display();

    }

    //超级文章管理
	public function super_article_manage(){
    	$article=M('Article');
    	$bigcategory=M('Bigcategory');
    	$cate=$bigcategory->select();
    	$this->assign('cate',$cate);
    	$plate=M('Plate');
    	$pid=$_GET['pid'];
    	if(isset($pid) && $pid != ''){
    		$adata=$article->where('pid='.$pid)->order('pid')->select();
    		$this->assign('b_id',$pid);
    	}else{
    		$adata=$article->order('pid')->select();
    	}
    	foreach ($adata as $k => $v){
	    	$adata[$k]['cate_data']=$bigcategory->where('id='.$v['pid'])->find();
//    		$adata[$k]['plate_data']=$plate->where('id='.$v['pid'])->find();
    	}
    	$this->assign('product',$adata);
    	$this->display('super_article_manage');

    }




	//添加文章
	public function article(){

		if($this->isPost()){
			$article=M('Article');
			if (get_magic_quotes_gpc()) {
				$_POST['content'] = isset($_POST['article_content'])?stripslashes($_POST['article_content']):'';
				$_POST['author_des'] = isset($_POST['author_des'])?stripslashes($_POST['author_des']):'';
			}
			else{
				$_POST['content'] =$_POST['article_content'];
			}
    		$_POST['add_time']=time();
    		$_POST['author']=$_SESSION['username'];
			$updata=$this->up(0,0);
    		if($updata){
				$_POST['author_img']=$updata[0]['savename'];
    		}
    		if(!$_POST['pid'] || !$_POST['title']){
    			return;
    		}
			if($article->where("pid=".$_POST['pid']." and title='".$_POST['title']."'")->find()){
    			echo "<script>alert('文章标题已存在，请填写其他文章标题');history.back();</script>";
    			return;
    		}
    		if($article->add($_POST)){
    			echo "<script>alert('添加成功');location.href='".__URL__."/article_manage';</script>";
    		}else{
    			echo "<script>alert('添加失败');history.back();</script>";
    		}

    	}else{
    		$bigcategory=M('Bigcategory');
    		$cate=$bigcategory->select();
    		$this->assign('cate',$cate);
    		$art_classify = M("ArticleClassify");
    		$ac_data=$art_classify->field('think_article_classify.*,b.big_category_name')->join('left join think_bigcategory b on b.id=think_article_classify.pid')->select();
    		$this->assign('ac_data',$ac_data);
    		$this->display();
    	}
    }

	//更改文章分类名称
     public function modify_article_classify(){

     	if($this->isPost()){
     		$id=isset($_POST['id'])?intval($_POST['id']):0;
     		if(!$id || !$_POST['pid'] || !$_POST['classify_name']){
     			return;
     		}
     		$updata=$this->up(0,0);
    		if($updata){
    			foreach($updata as $v){
    				if($v['key'] == 'bgimg'){
    					$_POST['bgimg']=$v['savename'];
    				}elseif($v['key'] == 'img'){
    					$_POST['img']=$v['savename'];
    				}
    			}
    		}
     		$article_classify=M('ArticleClassify');
     		if($article_classify->where("id=$id")->save($_POST)){
     			echo "<script>alert('修改成功');location.href='".__URL__."/article_classify_manage';</script>";
     		}else{
     			echo "<script>alert('修改失败');history.back();</script>";
     		}
     	}else{
	     	$article_classify=M('ArticleClassify');
	     	$pro=M('Product');
	     	$id=$_GET['id'];
	     	if(!$id){
	     		return;
	     	}
	     	$data=$article_classify->field('think_article_classify.*,b.big_category_name')->join('left join think_bigcategory b on b.id=think_article_classify.pid')->where('think_article_classify.id='.$id)->find();
	     	$this->assign('art_classify',$data);
	     	$prodata = $pro->select();
	     	$this->assign('pdata',$prodata);
	     	$this->display();
     	}
     }


	//静态化单个文章分类页
    public function article_classify_statice(){
    header("Content-Type:text/html; charset=utf-8");
    	$id=$_GET['id'];
    	if(!$id){
    		return;
    	}
    	$ac=M('ArticleClassify');
    	$data=$ac->find($id);

    	if(!$data['statice_name']){
			echo $this->art_no_statice_name;
    		return;
    	}
		$p=file_get_contents($this->root_name.'/Index/howto/id/'.$id.'/pid/'.$data['pid']);

	   	if(file_put_contents($data['statice_name'].'.html', $p)){
	   		$ac->where('id='.$id)->save(array('is_statice'=>'1'));
	   		echo $this->statice_success;
	   		return ;
	   	}else{
			echo $this->statice_fail;
    	}
    }



	//删除静态化文章分类页
 	public function delete_article_classify_statice(){
 		header("Content-Type:text/html; charset=utf-8");
    	$id=$_GET['id'];
    	if(!$id){
    		return;
    	}
    	$article_classify=M('ArticleClassify');
    	$pdata=$article_classify->find($id);
 		//删除相关静态化文件
     	if(file_exists($pdata['statice_name'].'.html')){
     		unlink($pdata['statice_name'].'.html');
     	}else{
     		echo $this->file_noexist;
     		return;
     	}
     	$data['is_statice']=0;
    	if($article_classify->where("id=$id")->save($data)){
     		echo $this->dele_statice_success;
     		return;
     	}else{
			echo $this->dele_statice_fail;
			return;
     	}
    }



	//删除文章分类
    public function delete_article_classify(){
    	header("Content-Type:text/html; charset=utf-8");

     	$id=$_GET['id'];
      	if (!$id){
     		return;
     	}
     	$article_classify=M('ArticleClassify');
     	$ret=$article_classify->where("id=$id")->delete();
    	if(!$ret){
			echo $this->delete_fail;
			return;
     	}else{
			echo $this->delete_success;
     	}
     }

	  //修改文章
	public function modify_article(){
	header("Content-Type:text/html; charset=utf-8");

		if($this->isPost()){
			$article=M('Article');
			$id=isset($_POST['article_id'])?intval($_POST['article_id']):0;
			if (get_magic_quotes_gpc()) {
				$_POST['content'] = isset($_POST['article_content'])?stripslashes($_POST['article_content']):'';
				$_POST['author_des'] = isset($_POST['author_des'])?stripslashes($_POST['author_des']):'';
			}else{
				$_POST['content'] =$_POST['article_content'];
			}
			$updata=$this->up(0,0);
    		if($updata){
				$_POST['author_img']=$updata[0]['savename'];
    		}echo $_POST['cid'];
    		if(!$_POST['cid'] || !$id || !$_POST['title']){
    			return;
    		}echo 1;
			if($article->where("id!=".$id." and pid=".$_POST['pid']." and title='".$_POST['title']."'")->find()){
    			echo "<script>alert('文章标题已存在，请填写其他文章标题');history.back();</script>";
    			return;
    		}
    		if($article->where("id=".$id)->save($_POST)){
    			echo "<script>alert('修改成功');location.href='".__URL__."/article_manage';</script>";
    		}else{
    			echo "<script>alert('修改失败');history.back();</script>";
    		}

    	}else{
    		$article=M('Article');
    		$id=$_GET['id'];
    		$author=$_SESSION['username'];
    		$a=$article->where('id="'.$id.'" AND author="'.$author.'"')->find();

    		if(!$id){
    			return;
    		}
			if(!$a || $author=='admin'){
				echo "<script>location.href='".__URL__."/super_modify_article/id/".$id."';</script>";
				return;
			}

	    	$bigcategory=M('Bigcategory');
	    	$art_classify=M('ArticleClassify');
	    	$adata=$article->where('id="'.$id.'" AND author="'.$author.'"')->find();

		    $adata['cate_data']=$bigcategory->field('id,big_category_name')->select();
	    	$adata['ac_data']=$art_classify->field('think_article_classify.*,b.big_category_name')->join('left join think_bigcategory b on b.id=think_article_classify.pid')->select();
    		$this->assign('article',$adata);
    		$this->display();
    	}
    }


     //超级修改文章
	public function super_modify_article(){
	header("Content-Type:text/html; charset=utf-8");

		if($this->isPost()){
			$article=M('Article');
			$id=isset($_POST['article_id'])?intval($_POST['article_id']):0;
			if (get_magic_quotes_gpc()) {
				$_POST['content'] = isset($_POST['article_content'])?stripslashes($_POST['article_content']):'';
				$_POST['author_des'] = isset($_POST['author_des'])?stripslashes($_POST['author_des']):'';
			}else{
				$_POST['content'] =$_POST['article_content'];
			}
			$updata=$this->up(0,0);
    		if($updata){
				$_POST['author_img']=$updata[0]['savename'];
    		}
    		if(!$_POST['cid'] || !$id || !$_POST['title']){
    			return;
    		}
			if($article->where("id!=".$id." and pid=".$_POST['pid']." and title='".$_POST['title']."'")->find()){
    			echo "<script>alert('文章标题已存在，请填写其他文章标题');history.back();</script>";
    			return;
    		}
    		if($article->where("id=".$id)->save($_POST)){
    			echo "<script>alert('修改成功');location.href='".__URL__."/super_article_manage';</script>";
    		}else{
    			echo "<script>alert('修改失败');history.back();</script>";
    		}

    	}else{
    		$id=$_GET['id'];
    		if(!$id){
    			return;
    		}
    		$article=M('Article');
	    	$bigcategory=M('Bigcategory');
	    	$art_classify=M('ArticleClassify');
	    	$adata=$article->where('id='.$id)->find();

		    $adata['cate_data']=$bigcategory->field('id,big_category_name')->select();
	    	$adata['ac_data']=$art_classify->field('think_article_classify.*,b.big_category_name')->join('left join think_bigcategory b on b.id=think_article_classify.pid')->select();
    		$this->assign('article',$adata);
    		$this->display('super_modify_article');
    	}
    }



	  //静态化文章页
    public function article_statice(){
		header("Content-Type:text/html; charset=utf-8");
    	$id=$_GET['id'];
    	$plate_name=$_GET['name'];
    	if(!$id){
    		return;
    	}
    	$article=M('Article');
    	//$plate=M('Plate');
    	$data=$article->find($id);
    	//$pdata=$plate->find($data['pid']);

    	if(!$data['statice_name']){
//    		echo "<script>alert('文章添加静态化名称');location.href='".__URL__."/modify_article/id/".$id."';</script>";
			echo $this->art_no_statice_name;
    		return;
    	}
    	//file_get_contents($this->root_name.__ROOT__.'/Index/howto/id/'.$pdata['pid'].'/type/select');
		$p=file_get_contents($this->root_name.'/Index/article/id/'.$id);

	   	if(file_put_contents($data['statice_name'].'.html', $p)){
	   		$article->where('id='.$id)->save(array('is_statice'=>'1'));
//	   		echo "<script>alert('静态化成功');location.href='".__URL__."/article_manage';</script>";
	   		echo $this->statice_success;
	   		return ;
	   	}else{
//    			echo "<script>alert('静态化失败');history.back();</script>";
			echo $this->statice_fail;
    	}
    }




	//删除文章静态化栏目页
 	public function delete_article_statice(){
 		header("Content-Type:text/html; charset=utf-8");
    	$id=$_GET['id'];
    	if(!$id){
    		return;
    	}
    	$article=M('Article');
    	$adata=$article->find($id);
 		//删除相关静态化文件
     	if($adata['statice_name'] && file_exists($adata['statice_name'].'.html')){
     		unlink($adata['statice_name'].'.html');
     	}else{
			echo $this->file_noexist;
     		return;
     	}
     	$data['is_statice']=0;
    	if($article->where("id=$id")->save($data)){
     		echo $this->dele_statice_success;
     		return;
     	}else{
     		echo $this->dele_statice_fail;
     		return;
     	}
    }



	//删除文章
    public function delete_article(){
    	header("Content-Type:text/html; charset=utf-8");

     	$id=$_GET['id'];
      	if (!$id){
     		return;
     	}
     	$article=M('Article');
     	$adata=$article->find($id);
    	//删除大类同时删除相关静态化文件
     	if($adata['statice_name'] && file_exists($adata['statice_name'].'.html')){
     		unlink($adata['statice_name'].'.html');
     	}
     	$ret=$article->where("id=$id")->delete();
    	if(!$ret){
//     		echo "<script>alert('删除文章失败');history.back();</script>";
			echo $this->delete_fail;
			return;
     	}else{
//     		echo "<script>alert('删除文章成功');location.href='".__URL__."/article_manage';</script>";
			echo $this->delete_success;
     	}
     }



	//改变文章排序
	function changeAsort(){
     	$val = $_POST['val'];
     	$id = $_POST['id'];
     	$art=M('Article');
     	$sort['asort'] = $val;
     	if($art->where("id=".$id)->save($sort)){
     		echo 1;
     	}else{
     		echo 0;
     	}
     }
     
     function changeClassAsort(){
     	$val = $_POST['val'];
     	$id = $_POST['id'];
     	$art=M('ArticleClassify');
     	$sort['asort'] = $val;
     	if($art->where("id=".$id)->save($sort)){
     		echo 1;
     	}else{
     		echo 0;
     	}
     }


	 //上传文件
    private function up($width=0,$height=0){
    	$width=$width==0?'150':$width;
    	$height=$height==0?'100':$height;
    	import('@.Org.UploadFile');
			$upload=new UploadFile();
			$upload->maxSize='1000000';  //是指上传文件的大小，默认为-1,不限制上传文件大小bytes
			$upload->savePath='./Public/upload/';       //上传保存到什么地方？路径建议大家已主文件平级目录或者平级目录的子目录来保存
			$upload->saveRule=uniqid;    //上传文件的文件名保存规则  time uniqid  com_create_guid  uniqid
			//$upload->autoCheck=false   ;  //是否自动检测附件
			$upload->uploadReplace=true;     //如果存在同名文件是否进行覆盖
			$upload->allowExts=array('jpg','jpeg','png','gif');     //准许上传的文件后缀
			$upload->allowTypes=array('image/png','image/jpg','image/pjpeg','image/gif','image/jpeg');  //检测mime类型
			$upload->thumb=true;   //是否开启图片文件缩略
			$upload->thumbMaxWidth=$width;  //以字串格式来传，如果你希望有多个，那就在此处，用,分格，写上多个最大宽
			$upload->thumbMaxHeight=$height;	//最大高度
			$upload->thumbPrefix='s_,m_';//缩略图文件前缀
			//$upload->thumbSuffix='_s,_m';  //文件后缀
			//$upload->thumbPath='' ;  // 如果留空直接上传至
			//$upload->thumbFile   在数据库当中也存一个文件名即可
			$upload->thumbRemoveOrigin=0;  //如果生成缩略图，是否删除原图 1是0否
			//$upload->autoSub   是否使用子目录进行保存上传文件
			//$upload->subType=''   子目录创创方式默认为hash 也可以设为date
			//$upload->dateFormat  子目录方式date的指定日期格式
			//$upload->hashLevle

		if(!$upload->upload()) { //  上传错误 提示错误信息

			//$this->error($upload->getErrorMsg());
			return false;

		}else{ //  上传成功 获取上传文件信息

			return $upload->getUploadFileInfo();

		 }
    }

    public function changeStatus(){
    	header("Content-Type:text/html; charset=utf-8");
    	$id=$_POST['id'];
    	if (!$id){
    		return;
    	}
    	$article=M('Article');
    	$pdata=$article->find($id);
    	$status = $pdata['status']==1?0:1;
    	//删除大类同时删除相关静态化文件
    	//     	if($pdata['statice_name'] && file_exists($pdata['statice_name'].'.html')){
    	//     		unlink($pdata['statice_name'].'.html');
    	//     	}
    	$ret=$article->where("id=$id")->save(array('status'=>$status));
    	if(!$ret){
    		echo 2;
    	}else{
    		echo $status;
    	}
    }

}